﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace DentalSoftware
{
    public partial class HinhAnhRang : UserControl
    {
        private Color[] dsMau;

        private TinhTrangRang rang;
        public TinhTrangRang Rang
        {
            get { return rang; }
            set { rang = value; }
        }

        public HinhAnhRang()
        {
            InitializeComponent();
        }

        public void SetTinhTrangRang(int[] tt)
        {            
            for (int i = 0; i < 5; i++)
            {
                rang.TinhTrang[i] = tt[i] % 7;
            }
            this.Invalidate();
        }
        public void SetTinhTrangRang(int mat, int tt)
        {
            rang.TinhTrang[mat] = tt % 7;
            this.Invalidate();
        }

        private void HinhAnhRang_Load(object sender, EventArgs e)
        {
            rang = new TinhTrangRang();
            rang.TinhTrang = new int[5];
            rang.Ghichu = new string[5];

            for (int i = 0; i < 5; i++)
            {
                rang.TinhTrang[i] = 0;
            }

            KhoiTaoDSMau();
        }            

        private void KhoiTaoDSMau()
        {
            dsMau = new Color[7];
            dsMau[0] = Color.White;
            dsMau[1] = Color.Gray;
            dsMau[2] = Color.Yellow;
            dsMau[3] = Color.Red;
            dsMau[4] = Color.GreenYellow;
            dsMau[5] = Color.Brown;
            dsMau[6] = Color.Purple;
        
        }

        public void VeMatTrai(Graphics g, Color mauVien, Color mauNen)
        {
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 1, 15, 48), Color.White, mauNen, LinearGradientMode.Horizontal);
            Pen pen = new Pen(mauVien);

            Point[] bien = new Point[4];
            bien[0] = new Point(0, 1);
            bien[1] = new Point(15, 16);
            bien[2] = new Point(15, 33);
            bien[3] = new Point(0, 48);
            
            g.FillPolygon(brush, bien);
            g.DrawPolygon(pen, bien);
            
        }
        public void VeMatPhai(Graphics g, Color mauVien, Color mauNen)
        {
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(49, 1, -15, 48), Color.White, mauNen, LinearGradientMode.Horizontal);
            Pen pen = new Pen(mauVien);

            Point[] bien = new Point[4];
            bien[0] = new Point(49, 1);
            bien[1] = new Point(34, 16);
            bien[2] = new Point(34, 33);
            bien[3] = new Point(49, 48);          

            g.FillPolygon(brush, bien);
            g.DrawPolygon(pen, bien);
        }
        public void VeMatTren(Graphics g, Color mauVien, Color mauNen)
        {
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(1, 0, 48, 15), Color.White, mauNen, LinearGradientMode.Vertical);
            Pen pen = new Pen(mauVien);

            Point[] bien = new Point[4];
            bien[0] = new Point(1, 0);
            bien[1] = new Point(16, 15);
            bien[2] = new Point(33, 15);
            bien[3] = new Point(48, 0);

            g.FillPolygon(brush, bien);
            g.DrawPolygon(pen, bien);

        }
        public void VeMatDuoi(Graphics g, Color mauVien, Color mauNen)
        {
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(1, 49, 48, -15), Color.White, mauNen, LinearGradientMode.Vertical);
            Pen pen = new Pen(mauVien);

            Point[] bien = new Point[4];
            bien[0] = new Point(1, 49);
            bien[1] = new Point(16, 34);
            bien[2] = new Point(33, 34);
            bien[3] = new Point(48, 49);

            g.FillPolygon(brush, bien);
            g.DrawPolygon(pen, bien);
        }
        public void VeMatGiua(Graphics g, Color mauVien, Color mauNen)
        {
            SolidBrush brush = new SolidBrush(mauNen);
            Pen pen = new Pen(mauVien);            

            g.FillRectangle(brush,17,17,15,15);
            g.DrawRectangle(pen, 17, 17, 15, 15);
        }       

        private bool IsMatTraiClicked(Point p)
        {
            if (p.X <= 15 && p.Y >= p.X + 1 && p.Y <= 48 - p.X)
                return true;
            return false;
        }
        private bool IsMatPhaiClicked(Point p)
        {
            if (p.X >= 34 && p.Y >= 50 - p.X && p.Y <= p.X - 1)
                return true;
            return false;
        }
        private bool IsMatTrenClicked(Point p)
        {
            if (p.Y <= 15 && p.X >= p.Y + 1 && p.X <= 48 - p.Y)
                return true;
            return false;
        }
        private bool IsMatDuoiClicked(Point p)
        {
            if (p.Y >= 34 && p.X >= 50 - p.Y && p.X <= p.Y - 1)
                return true;
            return false;
        }
        private bool IsMatGiuaClicked(Point p)
        {
            if (p.X >= 17 && p.X <= 32 && p.Y >= 17 && p.Y <= 32)
                return true;
            return false;
        }      

        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);
            Graphics g = e.Graphics;            

            VeMatTrai(g, Color.Blue, dsMau[rang.TinhTrang[0]]);
            VeMatPhai(g, Color.Blue, dsMau[rang.TinhTrang[1]]);
            VeMatTren(g, Color.Blue, dsMau[rang.TinhTrang[2]]);
            VeMatDuoi(g, Color.Blue, dsMau[rang.TinhTrang[3]]);
            VeMatGiua(g, Color.Blue, dsMau[rang.TinhTrang[4]]);

            g.Dispose();
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
        }

        public int ThayDoiTrangThai(Point mousepoint)
        {
            int vt = -1;

            if (IsMatTraiClicked(mousepoint))
            {
                rang.TinhTrang[0] = (rang.TinhTrang[0] + 1) % 7;
                vt = 0;
            }
            if (IsMatPhaiClicked(mousepoint))
            {
                rang.TinhTrang[1] = (rang.TinhTrang[1] + 1) % 7;
                vt = 1;
            }
            if (IsMatTrenClicked(mousepoint))
            {
                rang.TinhTrang[2] = (rang.TinhTrang[2] + 1) % 7;
                vt = 2;
            }
            if (IsMatDuoiClicked(mousepoint))
            {
                rang.TinhTrang[3] = (rang.TinhTrang[3] + 1) % 7;
                vt = 3;
            }
            if (IsMatGiuaClicked(mousepoint))
            {
                rang.TinhTrang[4] = (rang.TinhTrang[4] + 1) % 7;
                vt = 4;
            }

            this.Invalidate();

            return vt;
        }
    }
}
